//
//  HFFanClubJoinRollView.m
//  CBT
//
//  Created by 四点钟 on 2018/11/26.
//  Copyright © 2018 Mr.Zhang. All rights reserved.
//

#import "HFFanClubJoinRollView.h"
#import "NSTimer+CXExtension.h"

#define DefaultPurpleColor UIColorFromRGB(0x9a2ef6)

@interface HFFanClubJoinRollView()

@property (copy, nonatomic) NSMutableArray *newsArray;

@property (copy, nonatomic) NSMutableArray *newsRightArray;

@property (strong, nonatomic) UIView *contentView;

@property (strong, nonatomic) UILabel *showLabel;

@property (strong, nonatomic) UILabel *bottomLabel;

@property (strong, nonatomic) UILabel *showRightLabel;

@property (strong, nonatomic) UILabel *bottomRightLabel;

@property (assign, nonatomic) NSInteger showIndex;

@property (assign, nonatomic) NSInteger willShowIndex;

@property (nonatomic,strong) NSTimer *timer;

@property (nonatomic,strong) UIButton *actionButton;

@end

@implementation HFFanClubJoinRollView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self _setup];
    }
    return self;
}

- (void)setupTimer{
    
    __weak HFFanClubJoinRollView *weakSelf = self;
    self.timer = [NSTimer cx_scheduledTimerWithTimeInterval:5 repeats:YES fireBlock:^{
        [weakSelf updateTime];
    }];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)stopTimer
{
    [self.timer invalidate];
    self.timer = nil;
}

#pragma mark - action

- (void)updateTime
{
    [self startRolls];
}

- (void)setNewArray:(NSArray *)array rightArr:(NSArray *)rightArr
{
    if (array.count > 1) {
        [self stopTimer];
        [self setupTimer];
    }else{
        [self stopTimer];
    }
    _newsArray = [array mutableCopy];
    _newsRightArray = [rightArr mutableCopy];
    // 根据数据源显示起始内容
    _showIndex = 0;
    if (array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0){
        _willShowIndex = 0;
    }else{
        _willShowIndex = (_showIndex + 1) % array.count;
    }
    
    [self _updateLabels];
}

- (void)setNewsTextColor:(UIColor *)color {
    if (color) {
        _showLabel.textColor = color;
        _bottomLabel.textColor = color;
        _showRightLabel.textColor = color;
        _bottomRightLabel.textColor = color;
    }
}

- (void)setNewsFont:(UIFont *)font {
    if (font) {
        _bottomLabel.font = font;
        _showLabel.font = font;
        _bottomRightLabel.font = font;
        _showRightLabel.font = font;
    }
}

- (void)startRolls {
    _showIndex ++ ;
    _willShowIndex ++ ;
    _showIndex = _showIndex % _newsArray.count;
    _willShowIndex = _willShowIndex % _newsArray.count;
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.5 animations:^{
        CGFloat height = -weakSelf.contentView.bounds.size.height;
        weakSelf.showLabel.transform = CGAffineTransformMakeTranslation(0, height);
        weakSelf.bottomLabel.transform = CGAffineTransformMakeTranslation(0, height);
        weakSelf.showRightLabel.transform = CGAffineTransformMakeTranslation(0, height);
        weakSelf.bottomRightLabel.transform = CGAffineTransformMakeTranslation(0, height);
    } completion:^(BOOL finished) {
        weakSelf.showLabel.text = weakSelf.newsArray[weakSelf.showIndex];
        weakSelf.showLabel.transform = CGAffineTransformIdentity;
         weakSelf.bottomLabel.text = weakSelf.newsArray[weakSelf.willShowIndex];
        weakSelf.bottomLabel.transform = CGAffineTransformIdentity;
        
        weakSelf.showRightLabel.text = weakSelf.newsRightArray[weakSelf.showIndex];
        weakSelf.showRightLabel.transform = CGAffineTransformIdentity;
        weakSelf.bottomRightLabel.text = weakSelf.newsRightArray[weakSelf.willShowIndex];
        weakSelf.bottomRightLabel.transform = CGAffineTransformIdentity;
    }];
}

#pragma mark - setup

- (void)_updateLabels {
    if (self.newsArray == nil || [self.newsArray isKindOfClass:[NSNull class]] || self.newsArray.count == 0){
        _showLabel.text = @"";
        _bottomLabel.text = @"";
        _showRightLabel.text = @"";
        _bottomRightLabel.text = @"";
    }else{
        _showLabel.text = _newsArray[_showIndex];
        _bottomLabel.text = _newsArray[_willShowIndex];
        _showRightLabel.text = _newsRightArray[_showIndex];
        _bottomRightLabel.text = _newsRightArray[_showIndex];
    }
   
}

- (UILabel *)_createLabel {
    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:15.0];
    label.textColor = DefaultPurpleColor;
    label.userInteractionEnabled = YES;
    label.textAlignment = NSTextAlignmentLeft;
    label.backgroundColor = [UIColor clearColor];
    return label;
}

- (void)tapIt:(UITapGestureRecognizer *)gester {
    if (self.RollViewBlock != nil) {
        self.RollViewBlock(self.showIndex);
    }
}
- (void)_setup {
    self.clipsToBounds = YES;
    //__weak typeof(self) weakSelf = self;
    _contentView = [[UIView alloc] init];
    _contentView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapIt:)];
    [_contentView addGestureRecognizer:tap];
    
//    [_contentView bk_whenTapped:^{
//        if (weakSelf.RollViewBlock != nil) {
//            weakSelf.RollViewBlock(weakSelf.showIndex);
//        }
//    }];
    [self addSubview:_contentView];
    
    _showLabel = [self _createLabel];
    _bottomLabel = [self _createLabel];
    [_contentView addSubview:_showLabel];
    [_contentView addSubview:_bottomLabel];
    
    _showRightLabel = [self _createLabel];
    _bottomRightLabel = [self _createLabel];
    [_contentView addSubview:_showRightLabel];
    [_contentView addSubview:_bottomRightLabel];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _contentView.frame = self.bounds;
    CGRect frame = _contentView.frame;
    _showLabel.frame = CGRectMake(0, 0, frame.size.width/1.8, frame.size.height);
    _bottomLabel.frame = CGRectMake(0, frame.size.height, frame.size.width/1.8, frame.size.height);
    _showRightLabel.frame = CGRectMake(frame.size.width-180, 0, frame.size.width, frame.size.height);
    _bottomRightLabel.frame = CGRectMake(frame.size.width-180, frame.size.height, frame.size.width, frame.size.height);
}

-(void)dealloc
{
    NSLog(@"首页滚动试图释放");
    [self.timer invalidate];
    self.timer = nil;
}

@end
